package tool

import java.io.File
import java.nio.file.Files

import implicits.Implicits._
import org.apache.commons.io.FileUtils
import org.joda.time.DateTime
import tool.Pojo._

import scala.collection.JavaConverters._
import scala.xml.{Node, XML}

/**
 * Created by Administrator on 2019/12/5
 */
object Tool extends ProjectTool  {

  val splitFileSuffix = ".fq.gz"

  val dbName = "forensic_database"
  val windowsPath = s"C:\\${dbName}"
  val playPath = new File("../").getAbsolutePath
  val linuxPath = playPath + s"/${dbName}"
  val isWindows = {
    if (new File(windowsPath).exists()) true else false
  }
  val path = {
    if (new File(windowsPath).exists()) windowsPath else linuxPath
  }
  val windowsTestDir = new File("C:\\temp")
  val linuxTestDir = new File(playPath, "workspace")
  val testDir = if (windowsTestDir.exists()) windowsTestDir else linuxTestDir
  val exampleDir = new File(path, "example")
  val userDir = new File(path, "user")
  val binDir = new File(path, "bin")
  val dataDir = new File(path, "data")
  val fastxBinDir = new File(binDir, "fastx_toolkit/bin")

  val jarPath = {
    val inPath = "C:\\workspaceForIDEA\\forensic_fast\\server\\jars"
    val linuxInPath = linuxPath + "/jars"
    if (new File(inPath).exists()) inPath else linuxInPath
  }
  val yarnJarDir = new File(jarPath, "yarn")
  val setAJarDir = new File(jarPath, "setA")

  val pyPath = {
    val inPath = "C:\\workspaceForIDEA\\forensic_fast\\server\\pyScripts"
    val linuxInPath = linuxPath + "/pyScripts"
    if (new File(inPath).exists()) inPath else linuxInPath
  }

  def getInfoByFile(file: File) = {
    val lines = FileUtils.readLines(file).asScala
    val columnNames = lines.head.split("\t").drop(1)
    val array = lines.drop(1).map { line =>
      val columns = line.split("\t")
      val map = Map("geneId" -> columns(0))
      val otherMap = columnNames.zip(columns.drop(1)).map { case (columnName, data) =>
        (columnName -> data)
      }.toMap
      map ++ otherMap
    }
    (columnNames, array)
  }

  def generateMissionName = {
    (new DateTime).toString("yyyyMMddHHmmss")
  }

  def getUserIdDir(userId: Int) = {
    new File(Tool.userDir, userId.toString)
  }

  def getLogFile(dir: File) = {
    val file = new File(dir, "log.txt")
    "Run successfully!".toFile(file)
    file
  }

  def getInputXmlFile(dir: File) = {
    new File(dir, "input.xml")
  }

  def getInputTxtFile(dir: File) = {
    new File(dir, "input.txt")
  }

  def getGzFile(dir: File) = {
    new File(dir, "data.gz")
  }

  def getIndexFile(dir: File) = {
    new File(dir, "index.txt")
  }

  def getSplitWorkspaceDir(dir: File) = {
    new File(dir, "split")
  }

  def getFyWorkspaceDir(dir: File) = {
    new File(dir, "fy")
  }

  def getSplitResultDir(dir: File) = {
    new File(dir, "out")
  }

  def getOutputXmlFile(dir: File) = {
    new File(dir, "output.xml")
  }

  def getConfigFile(workspaceDir: File) = {
    new File(workspaceDir, "config.json")
  }

  def getSnpReadsFile(dir: File) = {
    new File(dir, "snp_reads.txt")
  }

  def getStrGenotypeFile(dir: File) = {
    new File(dir, "str_genotype.txt")
  }

  def getSnpGenotypeFile(dir: File) = {
    new File(dir, "snp_genotype.txt")
  }

  def getBasicFile(dir: File) = {
    new File(dir, "basic.txt")
  }

  def getStatFile(dir: File) = {
    new File(dir, "stat.txt")
  }

  def getUserSampleDir(userId: Int) = {
    val userIdDir = Tool.getUserIdDir(userId)
    new File(userIdDir, "sample")
  }

  def getSampleIdDir(userId: Int, sampleId: String) = {
    val userMissionFile = getUserSampleDir(userId)
    new File(userMissionFile, sampleId)
  }

}
